﻿package com.spx;

import java.io.InputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * 图像信息 数据存储对象
 * @author Administrator
 */
public final class Image{

	private Bitmap bitmap;
	private LGraphics g;
	private int width,height;
	private boolean isClose;


	public final static Image createImage(String fileName){
		return new Image(fileName);
	}

	public Image(String fileName){
		try{
			
			final InputStream is=new ArrayByteInput(fileName).getInputStream();
			if(is==null){
				throw new RuntimeException(fileName+" not found!");
			}
			BitmapFactory.Options opts=new BitmapFactory.Options();
			setBitmap(BitmapFactory.decodeStream(is,null,opts));
		}catch(Exception e){
			throw new RuntimeException(fileName+" not found!");
		}
	}

	public Image(Bitmap bitmap){
		setBitmap(bitmap);
	}

	private final void setBitmap(Bitmap bitmap){
		this.width=bitmap.getWidth();
		this.height=bitmap.getHeight();
		this.bitmap=bitmap;
	}

	public LGraphics getLGraphics(){
		if(g==null){
			g=new LGraphics(bitmap);
		}
		return g;
	}

	public Bitmap getBitmap(){
		return bitmap;
	}

	public int getWidth(){
		return width;
	}

	public int getHeight(){
		return height;
	}

	public final void dispose(){
		if(isClose)
			return;
		isClose=true;
		bitmap=null;
	}
}
